﻿////////////////////////////////////////////////////////////////////////
//                                                                    //
//                 Project: MediaElement Extensions                   //
//               Project URL: http://mext.codeplex.com                //
//                           GPL License v2                           //
//                                                                    //
//             Written by Geoffrey DANIEL { NeuroCypher }             //
//           Blog: http://blogs.developpeur.org/neurocypher           //
//                                                                    //
////////////////////////////////////////////////////////////////////////

using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using MediaElementExtensions.Controls.Interfaces;
using MediaElementExtensions.Controls.Managers;

namespace MediaElementExtensions.Controls.Bases
{

    public class ButtonMediaElementExtensionControlBase : Button, IMediaElementControl
    {

        #region DependencyProperties

        #region MediaElement

        public static DependencyProperty MediaElementProperty =
            DependencyProperty.Register("MediaElement",
                                        typeof(MediaElement),
                                        typeof(ButtonMediaElementExtensionControlBase),
                                        new PropertyMetadata(MediaElementChanged));

        public MediaElement MediaElement
        {
            get { return (MediaElement)GetValue(MediaElementProperty); }
            set { SetValue(MediaElementProperty, value); }
        }

        private static void MediaElementChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
        {

            var ctrl = o as IMediaElementControl;

            if (ctrl != null && ctrl.MediaElementManager != null)
            {
                ctrl.MediaElementManager.MediaElementPropertyChanged(e);
            }

        }

        #endregion

        #region AutoDisableWhenNoSourceLoaded

        public static DependencyProperty AutoDisableWhenNoSourceLoadedProperty =
            DependencyProperty.Register("AutoDisableWhenNoSourceLoaded",
                                        typeof(bool),
                                        typeof(ButtonMediaElementExtensionControlBase),
                                        new PropertyMetadata(AutoDisableWhenNoSourceLoadedChanged));

        public bool AutoDisableWhenNoSourceLoaded
        {
            get { return (bool)GetValue(AutoDisableWhenNoSourceLoadedProperty); }
            set { SetValue(AutoDisableWhenNoSourceLoadedProperty, value); }
        }

        private static void AutoDisableWhenNoSourceLoadedChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
        {

            var ctrl = o as IMediaElementControl;

            if (ctrl != null && ctrl.MediaElementManager != null)
            {
                ctrl.MediaElementManager.MediaElementPropertyChanged(e);
            }

        }

        #endregion

        #endregion

        #region MediaElementManager

        public MediaElementPropertyManager MediaElementManager { get; set; }

        #endregion

        #region Constructors

        public ButtonMediaElementExtensionControlBase()
        {
            
            InitializeBindings();

        }

        #endregion

        #region InitializeActions

        public void InitializeActions(
            System.Action aMediaElementChanged,
            System.Action<object, RoutedEventArgs> aMediaElementCurrentStateChanged,
            System.Action<object, RoutedEventArgs> aMediaElementDownloadProgressChanged,
            System.Action<object, RoutedEventArgs> aMediaElementMediaEnded,
            System.Action<object, ExceptionRoutedEventArgs> aMediaElementMediaFailed,
            System.Action<object, RoutedEventArgs> aMediaElementMediaOpened)
        {

            MediaElementManager = new MediaElementPropertyManager(
                aMediaElementChanged, 
                aMediaElementCurrentStateChanged, 
                aMediaElementDownloadProgressChanged, 
                aMediaElementMediaEnded, 
                aMediaElementMediaFailed, 
                aMediaElementMediaOpened);

        }

        #endregion

        #region InitializeBindings

        public virtual void InitializeBindings()
        {
            
        }

        #endregion

        #region CheckIsEnabled

        public virtual void CheckIsEnabled()
        {

            if (AutoDisableWhenNoSourceLoaded == true &&
                (MediaElement == null || MediaElement.CurrentState == MediaElementState.Closed))
            {
                IsEnabled = false;
            }
            else
            {
                IsEnabled = true;
            }

        }

        #endregion

    }

}
